package com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.rabbittrunk;

import com.esun.config.Config;
import com.esun.rabbit2.viewer.WebViewActivity;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuActGroup;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuChooseOkPage;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuChooseOkList;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.shiyixuanwu.ShiyixuanwuChoosePageAct;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * @ClassName: ShiyixuanwuContainer
 * @Description: (11选5承载类，显示各子页面共通数据)
 * @author Miko baoz@500wan.com
 * @date 2011-1-14 上午11:58:24
 * 
 */
public class ShiyixuanwuContainer extends Handler implements View.OnClickListener {

	public static ShiyixuanwuChooseBallSpace shiyixuanwuChooseBallSpace = null;

	// 整体ContainerView
	private View view;

	// MainAct
	private Context context;

	private Button btnRandom; // 机选按钮

	private Button btnChooseOK; // 选好了按钮

	private Button btnReset; // 清空按钮

	private Button btnPlayStyleChoose; // 玩法选择按钮

	private Button btnCancel;

	private ShiyixuanwuChooseOkPage chooseOk;

	private TextView tvTermInfoLeft; // 期号显示
	private TextView tvTermInfoMid; // 期号显示
	private TextView tvTermInfoRight; // 期号显示
	private TextView tvTimeLeft; // 剩余时间显示
	private TextView tvStyleTip; // 玩法提示

	private Button tvRecentTerms; // 最近开奖信息

	// 注数显示
	private TextView tvVoteLine;

	// 金额显示
	private TextView tvMoney;
	
	//记录机选注数
	private static int randomZhuShu = 0;
	
	public ShiyixuanwuContainer(View view, Context context) {
		this.view = view;
		this.context = context;

		initViews();

		shiyixuanwuChooseBallSpace = new ShiyixuanwuChooseBallSpace(context, view, ShiyixuanwuPlayStyle.getPlayStyle().getStyle(), ShiyixuanwuPlayStyle.getPlayStyle().getDetail());
	}

	private void initViews() {
		// 底部按钮
		btnRandom = (Button) view.findViewById(R.id.shiyixuanwu_container_btn_random);
		btnRandom.setOnClickListener(this);
		btnChooseOK = (Button) view.findViewById(R.id.shiyixuanwu_container_btn_ok);
		btnChooseOK.setOnClickListener(this);
		btnReset = (Button) view.findViewById(R.id.shiyixuanwu_container_btn_reset);
		btnReset.setOnClickListener(this);

		btnPlayStyleChoose = (Button) view.findViewById(R.id.shiyixuanwu_container_btn_playStyle);
		btnPlayStyleChoose.setOnClickListener(this);

		btnCancel = (Button) view.findViewById(R.id.shiyixuanwu_container_btn_cancel);
		btnCancel.setOnClickListener(this);

		tvRecentTerms = (Button) view.findViewById(R.id.shiyixuanwu_container_tv_recent);
		tvRecentTerms.setOnClickListener(this);

		tvStyleTip = (TextView) view.findViewById(R.id.shiyixuanwu_container_playTip);

		if (!ShiyixuanwuPlayStyle.getPlayStyle().getDetailTemp().equals(DataShare.NULLMARK)) {
			// btnPlayStyleChoose.setText(ShiyixuanwuPlayStyle.getPlayStyle().getDetailTemp());
			((TextView) view.findViewById(R.id.shiyixuanwu_choose_tv_playTip)).setText(ShiyixuanwuPlayStyle.getPlayStyle().getDetailTemp());
		}

		tvTermInfoLeft = (TextView) view.findViewById(R.id.shiyixuanwu_container_termInfoLeft);
		tvTermInfoMid = (TextView) view.findViewById(R.id.shiyixuanwu_container_termInfoMid);
		tvTermInfoRight = (TextView) view.findViewById(R.id.shiyixuanwu_container_termInfoRight);
		tvTimeLeft = (TextView) view.findViewById(R.id.shiyixuanwu_container_timeLeft);

		tvVoteLine = (TextView) view.findViewById(R.id.shiyixuanwu_container_tv_voteLine);
		ShiyixuanwuBuyingInfo.getBuyingInfo().setTvVoteLine(tvVoteLine);

		tvMoney = (TextView) view.findViewById(R.id.shiyixuanwu_container_tv_money);
		ShiyixuanwuBuyingInfo.getBuyingInfo().setTvMoney(tvMoney);

	}

	public View getView() {

		ShiyixuanwuChoosePageAct.pageFlag = 0;

		if (ShiyixuanwuChooseOkList.getChooseList().getSize() > 0) {
			btnCancel.setVisibility(View.VISIBLE);
		} else {
			btnCancel.setVisibility(View.GONE);
		}

		return view;

	}

	public void setNoBtnCancel() {
		btnCancel.setVisibility(View.GONE);
	}

	public void doRandom() {

		EDebug.o(this, "doing  Random");
		if (shiyixuanwuChooseBallSpace != null) {
			int howMany = 0;

			EDebug.o(this, "Random Button Pressed 11111111111" + String.valueOf(ShiyixuanwuPlayStyle.getPlayStyle().getStyle() != 0 && ShiyixuanwuPlayStyle.getPlayStyle().getDetail() != 0));
			if (ShiyixuanwuPlayStyle.getPlayStyle().getStyle() != 0 && ShiyixuanwuPlayStyle.getPlayStyle().getDetail() != 0) {
				howMany = ShiyixuanwuPlayStyle.getPlayStyle().getStyle();
				EDebug.o(this, "Random Button Pressed 22222222222222" + String.valueOf(howMany != 0));

				StringBuilder sbRandomNum = new StringBuilder();
				if (howMany != 0) {
					ShiyixuanwuBuyingInfo.getBuyingInfo().setVoteLine(1);
					sbRandomNum = LotteryUtils.randomNum(howMany, 11);

				}
				shiyixuanwuChooseBallSpace.reLoad(context, view, sbRandomNum);
			}

		}
		ShiyixuanwuBuyingInfo.getBuyingInfo().showVoteLine();
		ShiyixuanwuBuyingInfo.getBuyingInfo().showMoney();
	}

	public void doBtnRandom() {

		View.OnClickListener listener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.viewerutildialogshowrandomdialog_btn_ok:
					int RandomCount = ViewerUtil.getRandomCount();
					for (int i = 0; i < RandomCount; i++) {
						chooseOk.addOneRandom();
					}
					chooseOk.refresh();
					ViewerUtil.dismissRandomDialog();
					randomZhuShu = RandomCount;
					((Activity) context).setContentView(chooseOk.getView());

					/** 2011-8-1 city add for agp title change */
					Message msg = ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.obtainMessage(ShiyixuanwuActGroup.IN_LIST_VIEW, null);
					ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.sendMessage(msg);

					break;

				default:
					break;
				}
			}
		};

		ViewerUtil.showRandomDialog(context, "请输入机选注数", 1, 10, 10,randomZhuShu, listener);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.shiyixuanwu_container_btn_reset:

			ShiyixuanwuContainer.shiyixuanwuChooseBallSpace = new ShiyixuanwuChooseBallSpace(context, view, ShiyixuanwuPlayStyle.getPlayStyle().getStyle(), ShiyixuanwuPlayStyle.getPlayStyle()
					.getDetail());

			ShiyixuanwuBuyingInfo.getBuyingInfo().replaceAllData();

			break;

		// 机选
		case R.id.shiyixuanwu_container_btn_random:

			doBtnRandom();

			break;

		case R.id.shiyixuanwu_container_btn_ok:

			String strBuyOK = ShiyixuanwuBuyingInfo.getBuyingInfo().getBuyNum();
			int voteLine = ShiyixuanwuBuyingInfo.getBuyingInfo().getVoteLine();
			String playID = ShiyixuanwuBuyingInfo.getBuyingInfo().getPlayingID();

			if (voteLine < 1) {
				ViewerUtil.showTipDialog(context, "至少选一注", false);
			} else {
				ShiyixuanwuChooseOkList.getChooseList().add(playID, strBuyOK, voteLine, voteLine * 2, String.valueOf(ShiyixuanwuPlayStyle.getPlayStyle().getStyle()),
						String.valueOf(ShiyixuanwuPlayStyle.getPlayStyle().getDetail()));
				ShiyixuanwuChooseOkList.getChooseList().clearDeleteInfo();
				ShiyixuanwuChooseOkList.getChooseList().print();

				chooseOk.refresh();
				((Activity) context).setContentView(chooseOk.getView());

				/** 2011-8-1 city add for agp title change */
				Message msg = ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.obtainMessage(ShiyixuanwuActGroup.IN_LIST_VIEW, null);
				ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.sendMessage(msg);
			}

			break;

		case R.id.shiyixuanwu_container_btn_playStyle:

			ChooseStylePopAlert alert = new ChooseStylePopAlert(context, view, btnPlayStyleChoose);
			alert.alertShow();
			break;

		case R.id.shiyixuanwu_container_btn_cancel:
			if (ShiyixuanwuChooseOkList.getChooseList().getDeleteNum().length() > 0) {
				String strDelNum = ShiyixuanwuChooseOkList.getChooseList().getDeleteNum();
				String strDelPlayStyle = ShiyixuanwuChooseOkList.getChooseList().getDeletePlayStyle();
				int strDelVoteNum = Integer.valueOf(ShiyixuanwuChooseOkList.getChooseList().getDeleteVoteNum());
				int strDelVoteMoney = Integer.valueOf(ShiyixuanwuChooseOkList.getChooseList().getDeleteVoteMoney());

				ShiyixuanwuChooseOkList.getChooseList().add(strDelPlayStyle, strDelNum, strDelVoteNum, strDelVoteMoney, String.valueOf(ShiyixuanwuPlayStyle.getPlayStyle().getStyle()),
						String.valueOf(ShiyixuanwuPlayStyle.getPlayStyle().getDetail()));

				ShiyixuanwuChooseOkList.getChooseList().clearDeleteInfo();
				ShiyixuanwuChooseOkList.getChooseList().print();

				chooseOk.refresh();
			}
			((Activity) context).setContentView(chooseOk.getView());

			/** 2011-8-1 city add for agp title change */
			Message msg = ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.obtainMessage(ShiyixuanwuActGroup.IN_LIST_VIEW, null);
			ShiyixuanwuActGroup.handlerShiyixuanwuActGroup.sendMessage(msg);

			break;

		case R.id.shiyixuanwu_container_tv_recent:

			context.startActivity(new Intent(context, WebViewActivity.class).putExtra("url", Config.SHIYIXUANWU_RECENT_FIVE).putExtra("title", "近期开奖信息"));

			break;

		default:
			break;
		}
	}

	public void updateTerm(String term, boolean isSucceed) {
		if (isSucceed) {
			tvTermInfoLeft.setText("距");
			tvTermInfoMid.setText(String.valueOf(term));
			tvTermInfoRight.setText("期截止时间还有");
		} else {
			tvTermInfoLeft.setText(term);
		}
	}

	public void refreshTimeLeft(String str) {
		tvTimeLeft.setText(str);
	}

	public void setChoosePage(ShiyixuanwuChooseOkPage chooseOK) {
		this.chooseOk = chooseOK;
	}

	public void setPlayType(int style, int detail, String strLineNum) {

		if (strLineNum == null) {
			ShiyixuanwuPlayStyle.getPlayStyle().setStyle(style);
			ShiyixuanwuPlayStyle.getPlayStyle().setDetail(detail);
			ShiyixuanwuBuyingInfo.getBuyingInfo().replaceAllData();
			shiyixuanwuChooseBallSpace.reLoad(context, view, null);
			// 不需要切换“玩法切换按钮”上的文字，故注释------------billy-2012-02-23---------------------
			// btnPlayStyleChoose.setText(ShiyixuanwuPlayStyle.getPlayStyle().getButtonTxt(style, detail));
			tvStyleTip.setText(ShiyixuanwuPlayStyle.getPlayStyle().getTip());
		} else {

			ShiyixuanwuPlayStyle.getPlayStyle().setStyle(style);
			ShiyixuanwuPlayStyle.getPlayStyle().setDetail(detail);
			ShiyixuanwuBuyingInfo.getBuyingInfo().replaceAllData();
			shiyixuanwuChooseBallSpace.reLoad(context, view, null);
			// 不需要切换“玩法切换按钮”上的文字，故注释------------billy-2012-02-23---------------------
			// btnPlayStyleChoose.setText(ShiyixuanwuPlayStyle.getPlayStyle().getButtonTxt(style, detail));
			tvStyleTip.setText(ShiyixuanwuPlayStyle.getPlayStyle().getTip());
			editChoseLine(strLineNum);
		}
	}

	private void editChoseLine(String strLineNum) {

		EDebug.o(this, "【接收字符串】 " + strLineNum + "【接收字符串】 ");

		if (strLineNum.contains("-")) {

			EDebug.o(this, "【是否包含-】 " + true + "【是否包含 -】 ");

			String[] strTmp = strLineNum.split("\\|");

			shiyixuanwuChooseBallSpace.setNumSelected(strTmp[0].split(","), null, null);

			if (!strTmp[1].equals("-")) {
				shiyixuanwuChooseBallSpace.setNumSelected(null, strTmp[1].split(","), null);
			}

			if (!strTmp[2].equals("-")) {
				shiyixuanwuChooseBallSpace.setNumSelected(null, null, strTmp[2].split(","));
			}

		} else {

			EDebug.o(this, "【是否包含-】 " + false + "【是否包含 -】 ");
			shiyixuanwuChooseBallSpace.setNumSelected(strLineNum.split(","), null, null);
		}

	}

	@Override
	public void handleMessage(Message msg) {
		super.handleMessage(msg);

	}
}